package gui.batches;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observer;

import javax.swing.Timer;

import core.ItemFacade;
import core.ProductFacade;
import core.exception.NotFoundException;
import gui.common.*;

/**
 * Controller class for the remove item batch view.
 */
public class RemoveItemBatchController extends ItemBatchController implements
		IRemoveItemBatchController, Observer {
	
	private RemoveItemBatchView _theView;
	private ItemFacade _iFacade;
	private ProductFacade _pFacade;
	
	private boolean usingScanner;
	private boolean barcodeEmpty;
	
	//private List<Item> _allRemovedItems;
	//private ArrayList<ProductData> _removedProductData;
	
	private Timer barcodeTimer = new Timer(1000,
			new ActionListener() {
				public void actionPerformed(ActionEvent a) {
					removeItem();
				}
			}
	);
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the remove item batch view.
	 */
	public RemoveItemBatchController(IView view) {
		super(view);
		_theView = (RemoveItemBatchView) this._view;
		_iFacade = ItemFacade.instance();
		_iFacade.addObserver((Observer)this);
		_pFacade = ProductFacade.instance();
		_pFacade.addObserver((Observer)this);
		
		usingScanner = false;
		barcodeEmpty = true;
		construct();
	}
	
	/**
	 * Returns a reference to the view for this controller.
	 */
	@Override
	protected IRemoveItemBatchView getView() {
		return (IRemoveItemBatchView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		//TODO: code this method
		//add item button
		boolean enableRemoveButton = true;
		if(usingScanner || _theView.getBarcode().isEmpty()) {
			enableRemoveButton = false;
		}
		_theView.enableItemAction(enableRemoveButton);

		_theView.enableRedo(canRedo());
		_theView.enableUndo(canUndo());
		
	}

	/**
	 * This method is called when the "Item Barcode" field is changed
	 * in the remove item batch view by the user.
	 */
	@Override
	public void barcodeChanged() {
		barcodeEmpty = _theView.getBarcode().length() == 0;
		if (usingScanner) {

			if (barcodeEmpty) {
				barcodeTimer.stop();
			}
			else if (barcodeTimer.isRunning()) {
				barcodeTimer.restart();
			}
			else {
				barcodeTimer.start();
			}
			
		}
		enableComponents();
	}
	
	/**
	 * This method is called when the "Use Barcode Scanner" setting is changed
	 * in the remove item batch view by the user.
	 */
	@Override
	public void useScannerChanged() {
		usingScanner = _theView.getUseScanner();
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "Remove Item" button
	 * in the remove item batch view.
	 */
	@Override
	public void removeItem() {
		if(!_iFacade.containsItem(_theView.getBarcode())){
			_theView.displayErrorMessage("The entered barcode does not exist");
			_theView.setBarcode("");
			barcodeTimer.stop();
			enableComponents();
			return;
		}
		
		//remove it from the iFacade
		try {
			RemoveItemCommand command = new 
					RemoveItemCommand(_iFacade.getItem(_theView.getBarcode()));
			doCommand(command);
		} catch (NotFoundException e1) {
			getView().displayErrorMessage(e1.getMessage());
		}

		//give focus to the ItemBarcode field
		_theView.giveBarcodeFocus();
		_theView.setBarcode("");
		
		barcodeTimer.stop();
		
		loadValues();
	}

	/**
	 * This method is called when the user clicks the "Done" button
	 * in the remove item batch view.
	 */
	@Override
	public void done() {
		getView().close();
		onDone();
	}

}

